# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = 'restructuredtext en'
__all__ = []

from ... import pilelib as stack
from ...forms import napoleon as nlayout
from ...gamelib import state, info as gi
from ...packlib import info as cs
from .. import braid

GT, SL = gi.GT, gi.SL
Wrap = stack.StackWrapper


class AkbarsTriumph(cs.Mughal_Deck, braid.Braid):
    """
    Finish 1 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        ..pysollib.layouts.napolean.BraidSmallLayout
        - row [4 vert E reserve, 5 vert W reserve, no offset]
        - reserve [4 vert E foundation, 5 vert W foundation,, no offset]
        - braid [2 N, braid offset]
        - talon [1 S foundation, no offset]
        - waste [1 E talon, no offset]
        - foundation [4 vert W, 4 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon (max_rounds=3)
        Deal 1 card per waste on click. 2 redeal.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack
        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Foundation
        Init base card. Build in rank by same suit wrap from
        base card. Dir determined by 2nd card on any pile. Play top.

      _braid_ : pysollib.stack.Stack
        .. pysollib.stack.OpenStack (max_cards=12)
        Init deal 12 open card. Build none. Play top.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.RowStack
        Init 1 card. Build any from any but reserve or braid. Play
        top. Max 1 card.

      _reserve_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Reserve
        Init 1 card. Build any. Play top. Gap deal 1 card from braid. Max
        1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint
    """
    __info__ = dict(id=14412, name='Akbarʼs Triumph',
                    category=cs.TYPE_MUGHAL_GANJIFA,
                    game_type=GT.NAPOLEON, decks=1, redeals=2,
                    skill_level=SL.BALANCED, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _layout_ = nlayout.BraidLargeLayout
    _braid_ = Wrap(stack.OpenStack, max_cards=12)

    def _create_game(self, **kw):
        """
        _create_game([braid_cards=12]) -> Layout

        Add a canvas info label for base_rank next to talon.

        :returns: current layout with piles and their texts set.

        :seealso: `pysollib.layouts.napoleon` for keyword definitions.
        """
        kw.setdefault('braidcards', 12)
        return super(AkbarsTriumph, self)._create_game(**kw)

    @state.move_state('fill')
    def _auto_fill(self, stack):
        """
        While neighbor braid has cards, fill empty neighbor reserve with 1 of
        its card card else fill with 1 of other braid if has cards.
        """
        s = self.s
        if not stack.cards and stack in s['reserve']:
            force = stack in s['reserve'][5:]
            if s['braid'][int(force)].cards:  # Fill from nearest braid
                self.game.move(1, s['braid'][int(force)], stack)
            elif s['braid'][int(not force)].cards:  # Fill from other braid
                self.game.move(1, s['braid'][int(not force)], stack)


class AkbarsConquest(AkbarsTriumph):
    """
    Finish 2 deck on _foundation_.

    :CVariables:
      _layout_ : pysollib.layout.Layout
        ..pysollib.layouts.napolean.BraidSmallLayout
        - row [4 vert E reserve, 4 vert W reserve, no offset]
        - reserve [4 vert E foundation, 4 vert W foundation,, no offset]
        - braid [2 N, braid offset]
        - talon [1 S foundation, no offset]
        - waste [1 E talon, no offset]
        - foundation [8 over 2 vert W, 8 over 2 vert E, no offset]

      _talon_ : pysollib.stack.Stack
        .. pysollib.stack.WasteTalon (max_rounds=3)

        Deal 1 card per waste on click. 2 redeal.

      _waste_ : pysollib.stack.Stack
        .. pysollib.stack.WasteStack

        Init 1 card. Build none. Play top.

      _foundation_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Foundation

        Init base card. Build in suit by same rank wrap from
        base card. Dir determined by 2nd card on any pile. Play top.

      _braid_ : pysollib.stack.Stack
        .. pysollib.stack.OpenStack (max_cards=16)
        Init deal 16 open card. Build none. Play top.

      _row_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.RowStack

        Init 1 card. Build any from any but reserve or braid. Play
        top. Max 1 card.

      _reserve_ : pysollib.stack.Stack
        .. pysollib.stacks.napoleon.Reserve

        Init 1 card. Build any. Play top. Gap deal 1 card from braid. Max
        1 card.

      _hint_ : pysollib.hint.AbstractHint
        .. pysollib.hint.CautiousDefaultHint

    :todo: needs seperate layout
    """
    __info__ = dict(id=14413, name='Akbarʼs Conquest',
                    category=cs.TYPE_MUGHAL_GANJIFA,
                    game_type=GT.NAPOLEON, decks=2, redeals=2,
                    skill_level=SL.BALANCED, version='0.50fc')
    __author__ = 'Mt. Hood Playing Card'
    __version__ = 7.70

    _braid_ = Wrap(stack.OpenStack, max_cards=16)

    def _create_game(self, **kw):
        """
        _create_game([braid_cards=16]) -> Layout

        Add a canvas info label for base_rank next to talon.

        :returns: current layout with piles and their texts set.

        :seealso: `pysollib.layouts.napoleon` for keyword definitions.
        """
        kw.setdefault('braidcards', 16)
        return super(AkbarsConquest, self)._create_game(**kw)


PROVIDES = [AkbarsConquest, AkbarsTriumph]